package com.kili.cloud.shorturl.controller.config;

import com.github.pagehelper.PageInfo;
import com.kili.cloud.common.context.Result;
import com.kili.cloud.common.context.ResultStatus;
import com.kili.cloud.shorturl.controller.config.vo.UrlVo;
import com.kili.cloud.shorturl.service.ShortUrlFindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短链后台管理控制层
 * Abner.Hu 2019/08/13
 */
@RestController
@Slf4j
public class UrlListController {

    @Autowired
    ShortUrlFindService shortUrlFindService;

    @PostMapping("/Url/getUrlList")
    public Result<PageInfo<UrlVo>> getUrlList(UrlVo urlVo){

        Assert.notNull(urlVo,"urlVo is null");
        Result result = new Result();

        PageInfo<UrlVo> list = shortUrlFindService.findAllUrl(urlVo);

        result.setData(list);

        return result;
    }

    @PostMapping("/Url/cancleUrl")
    public Result cancleUrl(UrlVo urlVo){

        Result result = new Result();
        try {
            shortUrlFindService.cancleUrl(urlVo);
        }catch (Exception e) {
            log.error("取消失败", e);
            result.setResultStatus(ResultStatus.INTERNAL_SERVER_ERROR);
            return result;
        }
        result.setResultStatus(ResultStatus.OK);
        return result;
    }

}
